package com.dacong.nezha.service;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dacong.nezha.domain.R;
import com.dacong.nezha.utils.Shensu;
import com.dacong.nezha.utils.Tongji;
import com.dacong.nezha.utils.Xiadan;
import com.dacong.nezha.utils.Yanghao;
import javafx.application.Platform;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.condition.RequestConditionHolder;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ApiService {

    public Integer yanghaoyanshi = 2000;

    Integer xiadanyanshi = 3000;

    public String yanghao(String token){

        String s = Yanghao.yanghaoWait(token);
        JSONObject entries = JSONUtil.parseObj(s);
        JSONArray content = entries.getJSONArray("content");
        if (content.size() == 0) {
            return "CK 不足";
        }

        BigDecimal profile = Tongji.profile(token);

        if (profile != null && profile.compareTo(BigDecimal.valueOf(100L)) == -1){
            return "余额过低，暂停养号";
        }

        JSONObject jsonObject = content.getJSONObject(0);
        String ckId = jsonObject.getStr("id");
        Map<String, String> recipient = Yanghao.getMaintenanceRecipient(ckId,token);
        Yanghao.prepareMallOrder(ckId,0,token);
        ThreadUtil.sleep(yanghaoyanshi);
        Yanghao.prepareMallOrder(ckId,1,token);
        ThreadUtil.sleep(yanghaoyanshi);
        Yanghao.prepareMallOrder(ckId,2,token);
        ThreadUtil.sleep(yanghaoyanshi);
        Yanghao.prepareMallOrder(ckId,3,token);
        ThreadUtil.sleep(yanghaoyanshi);
        String mallOrder = Yanghao.createMallOrder(ckId, recipient,token);
        JSONObject resp = JSONUtil.parseObj(mallOrder);
        Integer code = resp.getInt("code");
        if (code != 0){
            return "养号失败 "+ resp.getStr("message");
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        log.info("机器码：{} 账户：{} 养号成功",request.getHeader("machineId"),request.getHeader("username"));
        ThreadUtil.sleep(yanghaoyanshi);
        Yanghao.prepareMallOrder(ckId,4,token);
        ThreadUtil.sleep(yanghaoyanshi);
        Yanghao.prepareMallOrder(ckId,5,token);
        ThreadUtil.sleep(yanghaoyanshi);
        Yanghao.prepareMallOrder(ckId,6,token);
        ThreadUtil.sleep(yanghaoyanshi);
        Yanghao.prepareMallOrder(ckId,7,token);

        return "养号成功";

    }

    public String xiadan(String token){
        String s = Xiadan.slinkList(token);

        JSONObject entries = JSONUtil.parseObj(s);
        JSONArray content = entries.getJSONArray("content");
        if (content.size() == 0){
            return "店铺不足，无法下单";
        }

        List<String> ckIdList = Yanghao.ckList(token);
        if (ckIdList.isEmpty()){
            return "养号数量不足，无法下单";
        }

        BigDecimal profile = Tongji.profile(token);

        if (profile != null && profile.compareTo(BigDecimal.valueOf(100L)) == -1){
            return "余额过低，暂停下单";
        }


        //反转养号完成数据  从最早租用的开始下单
        Collections.reverse(ckIdList);


        JSONObject jsonObject = content.getJSONObject(0);
        String slinkId = jsonObject.getStr("id");
        Map<String, String> recipient = Xiadan.slinkGetRecipient(slinkId,token);

        Xiadan.prepareMallOrder(ckIdList.get(0),0,token);
        ThreadUtil.sleep(xiadanyanshi);
        Xiadan.prepareMallOrder(ckIdList.get(0),1,token);
        ThreadUtil.sleep(xiadanyanshi);
        Xiadan.prepareMallOrder(ckIdList.get(0),2,token);
        ThreadUtil.sleep(xiadanyanshi);
        Xiadan.prepareMallOrder(ckIdList.get(0),3,token);
        ThreadUtil.sleep(xiadanyanshi);
        String mallOrder = Xiadan.createMallOrder(slinkId, ckIdList.get(0), recipient,token);
        JSONObject resp = JSONUtil.parseObj(mallOrder);
        Integer code = resp.getInt("code");
        if (code != 0){
            return "下单失败 "+resp.getStr("message");
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        log.info("机器码：{} 账户：{} 下单成功",request.getHeader("machineId"),request.getHeader("username"));
        ThreadUtil.sleep(xiadanyanshi);
        Xiadan.prepareMallOrder(ckIdList.get(0),4,token);
        ThreadUtil.sleep(xiadanyanshi);
        Xiadan.prepareMallOrder(ckIdList.get(0),5,token);
        ThreadUtil.sleep(xiadanyanshi);
        Xiadan.prepareMallOrder(ckIdList.get(0),6,token);
        ThreadUtil.sleep(xiadanyanshi);
        Xiadan.prepareMallOrder(ckIdList.get(0),7,token);
        ThreadUtil.sleep(xiadanyanshi);
        Xiadan.prepareMallOrder(ckIdList.get(0),100,token);
        return "下单成功";
    }
    public String fuhuo(String token){
        String s = Xiadan.slinkListFuhuo(token);
        JSONObject entries = JSONUtil.parseObj(s);
        Integer totalElements = entries.getInt("totalElements");
        if (totalElements == 0){
            return null;
        }
        JSONArray content = entries.getJSONArray("content");
        JSONObject jsonObject = content.getJSONObject(0);
        String id = jsonObject.getStr("id");
        String s1 = Xiadan.changeSlink(id,token);
        JSONObject resp = JSONUtil.parseObj(s1);
        Integer code = resp.getInt("code");
        if (code == 0){
            return "复活成功";
        }else{
            return "复活失败 "+resp.getStr("message");
        }
    }
    public String shensu(String token){
        String s = Shensu.mallOrderList(token);
        JSONObject entries = JSONUtil.parseObj(s);
        JSONArray content = entries.getJSONArray("content");
        Integer totalElements = entries.getInt("totalElements");
        if (totalElements == 0){
            return null;
        }

        JSONObject jsonObject = content.getJSONObject(0);
        String orderNo = jsonObject.getStr("orderNo");
        String str = Shensu.mallOrderApplyComplaint(orderNo,token);

        JSONObject resp = JSONUtil.parseObj(str);
        Integer code = resp.getInt("code");
        if (code == 0){
            return "申诉成功";
        }else{
            return "申诉失败 "+resp.getStr("message");
        }
    }
    public String tuikuan(String token){
        String s = Shensu.mallOrderList2(token);
        JSONObject entries = JSONUtil.parseObj(s);
        JSONArray content = entries.getJSONArray("content");
        Integer totalElements = entries.getInt("totalElements");
        if (totalElements == 0){
            return null;
        }
        JSONObject jsonObject = content.getJSONObject(0);
        String orderNo = jsonObject.getStr("orderNo");
        String str =  Shensu.mallOrderapplyRefund(orderNo,token);

        JSONObject resp = JSONUtil.parseObj(str);
        Integer code = resp.getInt("code");
        if (code == 0){
            return "退款成功";
        }else{
            return "退款失败 "+resp.getStr("message");
        }
    }

    public Map<String,Object> tongji(String token){
        int yanghao = Tongji.yanghaoWait(token);
        int yanghaoing = Tongji.yanghaoing(token);
        int yanghaoFinish = Tongji.yanghaoFinish(token);
        int yixiadan = Tongji.yixiadan(token);
        int shensuzhong = Tongji.shensuzhong(token);
        int tuikuanzhong = Tongji.tuikuanzhong(token);

        Integer zhunbeizhong = Tongji.zhunbeizhong(token);
        Integer todayYanghao = Tongji.todayYanghao(token);
        Integer todayXiadan = Tongji.todayXiadan(token);
        Integer todayPeifu = Tongji.todayPeifu(token);
        Integer todaytuikuan = Tongji.todaytuikuan(token);

        BigDecimal profile = Tongji.profile(token);
        double asset = profile.doubleValue();
        double yanghaoguzhi = yanghao * 3 + yanghaoing * 4.3 + yanghaoFinish * 4.3;
        double dingdanguzhi = zhunbeizhong * 2 + yixiadan * 45 +shensuzhong * 45 + tuikuanzhong * 35;

        Map<String,Object> data = new HashMap<>();
        data.put("yanghao",yanghao);
        data.put("yanghaoing",yanghaoing);
        data.put("yanghaoFinish",yanghaoFinish);
        data.put("yixiadan",yixiadan);
        data.put("shensuzhong",shensuzhong);
        data.put("tuikuanzhong",tuikuanzhong);
        data.put("zhunbeizhong",zhunbeizhong);
        data.put("todayYanghao",todayYanghao);
        data.put("todayXiadan",todayXiadan);
        data.put("todayPeifu",todayPeifu);
        data.put("todaytuikuan",todaytuikuan);
        data.put("profile",profile);
        data.put("guzhi",BigDecimal.valueOf(asset+yanghaoguzhi+dingdanguzhi).setScale(2,BigDecimal.ROUND_DOWN));
        return data;
    }

    public String buyCk(Integer num,String token){
        String s = Yanghao.buyCk(num,token);
        JSONObject entries = JSONUtil.parseObj(s);
        if (entries.getInt("code") == 0) {
            return "购买CK"+num+"条,等待发货";
        }
        return "购买失败";
    }

    public String buySlink(Integer num,String token){
        String s = Xiadan.buySlink(num,token);
        JSONObject entries = JSONUtil.parseObj(s);
        if (entries.getInt("code") == 0) {
            return "购买店铺"+num+"条,等待发货";
        }
        return "购买失败";
    }
}
